package com.example.ykbboom.broadcastpractice;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
 * @author ykbboom
 * @email ykbpro@whut.edu.cn
 * @time 2017-1-25 21:19:04
 * 功能: 登录
*/
public class LoginActivity extends BaseActivity {

    private EditText accountText;
    private EditText passwordText;
    private Button accountClearBtn;//清除账户名
    private Button passwordClearBtn;//清除密码
    private Button seePasswordBtn;//查看输入的密码
    private Button loginBtn;//登录
    private TextWatcher accountWatcher;//监控输入框
    private TextWatcher passwordWatcher;
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        if(getSupportActionBar()!=null){
            getSupportActionBar().hide();
        }
        accountText = (EditText) findViewById(R.id.account);
        passwordText = (EditText) findViewById(R.id.password);

        accountClearBtn = (Button)findViewById(R.id.account_clear);
        passwordClearBtn = (Button)findViewById(R.id.password_clear);
        seePasswordBtn = (Button)findViewById(R.id.password_eye);
        loginBtn = (Button)findViewById(R.id.login);
        initWatcher();
        accountText.addTextChangedListener(accountWatcher);//设置EditText监控事件
        passwordText.addTextChangedListener(passwordWatcher);//设置EditText监控事件
        sharedPreferences = getSharedPreferences("state",MODE_PRIVATE);
        if(sharedPreferences.getBoolean("login_state",false)){
            //之前已经登录过了
            Intent intent = new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
        }
        //清除输入的账户
        accountClearBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                accountText.setText("");
                passwordText.setText("");
            }
        });
        //清除输入的密码
        passwordClearBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                passwordText.setText("");
            }
        });
        //查看输入的密码
        seePasswordBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(passwordText.getInputType() == (InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD)){
                    seePasswordBtn.setBackgroundResource(R.drawable.closeeye);
                    passwordText.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_NORMAL);
                }else{
                    seePasswordBtn.setBackgroundResource(R.drawable.openeye);
                    passwordText.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                passwordText.setSelection(passwordText.getText().toString().length());
            }
        });
        //登录
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    String account = accountText.getText().toString();
                    String password = passwordText.getText().toString();
                    if(account.equals("123")&&password.equals("123")){
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putBoolean("login_state",true);
                        editor.apply();
                        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                        startActivity(intent);
                        finish();
                    }else{
                        Toast.makeText(LoginActivity.this,"账号或者密码错误。",Toast.LENGTH_SHORT).show();
                    }


            }
        });
    }

    private void initWatcher(){
        accountWatcher = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            public void afterTextChanged(Editable s) {
                if(s.toString().length()>0){
                    accountClearBtn.setVisibility(View.VISIBLE);
                }else{
                    accountClearBtn.setVisibility(View.INVISIBLE);
                }
            }
        };
            passwordWatcher = new TextWatcher() {
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            public void beforeTextChanged(CharSequence s, int start, int count,int after) {}
            public void afterTextChanged(Editable s) {
                if(s.toString().length()>0){
                    passwordClearBtn.setVisibility(View.VISIBLE);
                }else{
                    passwordClearBtn.setVisibility(View.INVISIBLE);
                }
            }
        };
    }
}
